//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "YieldTermStructure.h"
using namespace Cephei::QL::Termstructures;
#include <gen/QL/InterestRate.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Quote.h>
#include <gen/QL/TermStructure.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::CYieldTermStructure::CYieldTermStructure (boost::shared_ptr<QuantLib::YieldTermStructure>& childNative, Object^ owner) : CTermStructure(CYieldTermStructure::typeid)
{
#ifdef HANDLE
	_phYieldTermStructure = NULL;
#endif
	_ppYieldTermStructure = &childNative;
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppYieldTermStructure));
}
Cephei::QL::Termstructures::CYieldTermStructure::CYieldTermStructure (QuantLib::YieldTermStructure& childNative, Object^ owner) : CTermStructure(CYieldTermStructure::typeid)
{
#ifdef HANDLE
	_phYieldTermStructure = NULL;
#endif
	_ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (&childNative);
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppYieldTermStructure));
    _YieldTermStructureOwner = owner;
    _TermStructureOwner = owner;
}

Cephei::QL::Termstructures::CYieldTermStructure::CYieldTermStructure (CYieldTermStructure^ copy) : CTermStructure(CYieldTermStructure::typeid)
{
#ifdef HANDLE
	_phYieldTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (copy->GetShared());
        _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppYieldTermStructure));
    }
}
Cephei::QL::Termstructures::CYieldTermStructure::CYieldTermStructure (System::Type^ t) : CTermStructure(CYieldTermStructure::typeid)
{
#ifdef HANDLE
	_phYieldTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CYieldTermStructure::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::CYieldTermStructure::CYieldTermStructure (QuantLib::Handle<QuantLib::YieldTermStructure>& childNative, Object^ owner)  : CTermStructure(CYieldTermStructure::typeid)
{
	_phYieldTermStructure = &childNative;
	_ppYieldTermStructure = &static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>>(childNative.currentLink());
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppYieldTermStructure));
    _YieldTermStructureOwner = owner;
}
Cephei::QL::Termstructures::CYieldTermStructure::CYieldTermStructure (QuantLib::Handle<QuantLib::YieldTermStructure> childNative)  : CTermStructure(CYieldTermStructure::typeid)
{
	_phYieldTermStructure = &childNative;
	_ppYieldTermStructure = &static_cast<boost::shared_ptr<QuantLib::YieldTermStructure>>(childNative.currentLink());
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppYieldTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::CYieldTermStructure::CYieldTermStructure (QuantLib::YieldTermStructure childNative)  : CTermStructure(CYieldTermStructure::typeid)
{
#ifdef HANDLE
	_phYieldTermStructure = NULL;
#endif
	_ppYieldTermStructure = new boost::shared_ptr<QuantLib::YieldTermStructure> (new QuantLib::YieldTermStructure (childNative));
    _ppTermStructure = new boost::shared_ptr<QuantLib::TermStructure> (boost::dynamic_pointer_cast<QuantLib::TermStructure> (*_ppYieldTermStructure));
}
#endif

Cephei::QL::Termstructures::CYieldTermStructure::~CYieldTermStructure ()
{
    if (_ppYieldTermStructure != NULL)
    {
	    delete _ppYieldTermStructure;
        _ppYieldTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::CYieldTermStructure::!CYieldTermStructure ()
{
    if (_ppYieldTermStructure != NULL)
    {
	    delete _ppYieldTermStructure;
    }
}
QuantLib::YieldTermStructure& Cephei::QL::Termstructures::CYieldTermStructure::GetReference ()
{
    if (_ppYieldTermStructure == NULL) throw gcnew NativeNullException ();
	return **_ppYieldTermStructure;
}
boost::shared_ptr<QuantLib::YieldTermStructure>& Cephei::QL::Termstructures::CYieldTermStructure::GetShared ()
{
    if (_ppYieldTermStructure == NULL) throw gcnew NativeNullException ();
	return *_ppYieldTermStructure;
}
QuantLib::YieldTermStructure* Cephei::QL::Termstructures::CYieldTermStructure::GetPointer ()
{
    if (_ppYieldTermStructure == NULL) throw gcnew NativeNullException ();
	return &**_ppYieldTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YieldTermStructure>& Cephei::QL::Termstructures::CYieldTermStructure::GetHandle ()
{
	if (_phYieldTermStructure == NULL)
	{
		_phYieldTermStructure = new Handle<QuantLib::YieldTermStructure> (*_ppYieldTermStructure);
	}
	return *_phYieldTermStructure;
}
#endif
bool Cephei::QL::Termstructures::CYieldTermStructure::HasNative () 
{
	return (_ppYieldTermStructure != NULL);
}

Cephei::IVector<DateTime>^ Cephei::QL::Termstructures::CYieldTermStructure::JumpDates::get ()
{
    try
    {
    	std::vector<QuantLib::Date>& _rv = (std::vector<QuantLib::Date>&)(*_ppYieldTermStructure)->jumpDates ( );   
        Cephei::IVector<DateTime>^ _nrv = gcnew CoVector<DateTime> (gcnew CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Termstructures::CYieldTermStructure::JumpTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppYieldTermStructure)->jumpTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::CYieldTermStructure::Discount (Double t, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppYieldTermStructure)->discount ( _t,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::CYieldTermStructure::Discount (DateTime d, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppYieldTermStructure)->discount ( _d,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::Termstructures::CYieldTermStructure::ForwardRate (Double t1, Double t2, QL::CompoundingEnum comp, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ freq, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1);
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2);
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (freq) ? (QuantLib::Frequency)freq->Value : QuantLib::Annual ); //5
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppYieldTermStructure)->forwardRate ( _t1,  _t2,  _comp,  _freq,  _extrapolate );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::Termstructures::CYieldTermStructure::ForwardRate (DateTime d, Cephei::QL::Times::IPeriod^ p, Cephei::QL::Times::IDayCounter^ resultDayCounter, QL::CompoundingEnum comp, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ freq, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CPeriod^ _Cp;
    CDayCounter^ _CresultDayCounter;
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
        _Cp = safe_cast<CPeriod^> (p);
        _Cp->Lock();
        QuantLib::Period& _p = static_cast<QuantLib::Period&> (_Cp->GetReference ()); 
        _CresultDayCounter = safe_cast<CDayCounter^> (resultDayCounter);
        _CresultDayCounter->Lock();
        QuantLib::DayCounter& _resultDayCounter = static_cast<QuantLib::DayCounter&> (_CresultDayCounter->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (freq) ? (QuantLib::Frequency)freq->Value : QuantLib::Annual ); //5
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppYieldTermStructure)->forwardRate ( _d,  _p,  _resultDayCounter,  _comp,  _freq,  _extrapolate );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp != nullptr) _Cp->Unlock();
        if (_CresultDayCounter != nullptr) _CresultDayCounter->Unlock();
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::Termstructures::CYieldTermStructure::ForwardRate (DateTime d1, DateTime d2, Cephei::QL::Times::IDayCounter^ resultDayCounter, QL::CompoundingEnum comp, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ freq, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CDayCounter^ _CresultDayCounter;
    try
    {
        QuantLib::Date _d1 = (QuantLib::Date)ValueHelper::Convert (d1);
        QuantLib::Date _d2 = (QuantLib::Date)ValueHelper::Convert (d2);
        _CresultDayCounter = safe_cast<CDayCounter^> (resultDayCounter);
        _CresultDayCounter->Lock();
        QuantLib::DayCounter& _resultDayCounter = static_cast<QuantLib::DayCounter&> (_CresultDayCounter->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (freq) ? (QuantLib::Frequency)freq->Value : QuantLib::Annual ); //5
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppYieldTermStructure)->forwardRate ( _d1,  _d2,  _resultDayCounter,  _comp,  _freq,  _extrapolate );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CresultDayCounter != nullptr) _CresultDayCounter->Unlock();
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::Termstructures::CYieldTermStructure::ZeroRate (Double t, QL::CompoundingEnum comp, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ freq, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (freq) ? (QuantLib::Frequency)freq->Value : QuantLib::Annual ); //5
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppYieldTermStructure)->zeroRate ( _t,  _comp,  _freq,  _extrapolate );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::Termstructures::CYieldTermStructure::ZeroRate (DateTime d, Cephei::QL::Times::IDayCounter^ resultDayCounter, QL::CompoundingEnum comp, Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>^ freq, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    CDayCounter^ _CresultDayCounter;
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
        _CresultDayCounter = safe_cast<CDayCounter^> (resultDayCounter);
        _CresultDayCounter->Lock();
        QuantLib::DayCounter& _resultDayCounter = static_cast<QuantLib::DayCounter&> (_CresultDayCounter->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::FrequencyEnum>::IsSome::get (freq) ? (QuantLib::Frequency)freq->Value : QuantLib::Annual ); //5
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppYieldTermStructure)->zeroRate ( _d,  _resultDayCounter,  _comp,  _freq,  _extrapolate );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CresultDayCounter != nullptr) _CresultDayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Termstructures::CYieldTermStructure::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppYieldTermStructure)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

